package com.example.apidocserver.application.event.impl;

import com.example.apidocserver.application.event.SpaceEvent;
import com.example.apidocserver.domain.space.entity.Space;
import com.example.apidocserver.domain.space.entity.SpaceEnvironment;
import com.example.apidocserver.domain.space.service.SpaceEnvironmentService;
import com.example.apidocserver.domain.space.service.SpaceService;
import com.example.apidocserver.domain.user.entity.DocUsrHis;
import com.example.apidocserver.domain.user.event.UserDocInfoEventService;
import com.example.apidocserver.infrastructure.enums.HisIdEnum;
import com.example.apidocserver.infrastructure.util.user.UserUtil;
import com.example.apidocserver.interfaces.vo.SpaceEnvironmentVo;
import com.example.apidocserver.interfaces.vo.SpaceVo;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Objects;

/**
 * 空间相关处理事件
 *
 * @author xyqlg
 */
@Service
public class SpaceEventImpl implements SpaceEvent {
    @Resource
    private SpaceService spaceService;
    @Resource
    private SpaceEnvironmentService environmentServiceImpl;

    @Resource
    private UserDocInfoEventService userDocInfoEventService;

    @Override
    public SpaceVo getSpace(Boolean writeFlag) {
        List<Space> spaces = spaceService.queryAll(writeFlag);

        SpaceVo spaceVo = new SpaceVo();
        spaceVo.setSpaces(spaces);
        if (!CollectionUtils.isEmpty(spaces)) {
            spaces.sort(Comparator.comparing(Space::getOrderNo));

            // 当前空间信息使用用户最后访问空间
            setCurrentSpace(spaces, spaceVo);
        } else {
            spaceVo.setCurrentSpace(new Space());
        }
        spaceVo.setCreateFlag(UserUtil.createSpaceFlag());
        return spaceVo;
    }

    private void setCurrentSpace(List<Space> spaces, SpaceVo spaceVo) {
        if (CollectionUtils.isEmpty(spaces)) {
            return;
        }
        DocUsrHis docUsrHis = userDocInfoEventService.getUsrLastInfo();
        if (Objects.nonNull(docUsrHis) && Objects.nonNull(docUsrHis.getSpaceId())) {{
            for (Space space : spaces) {
                if (docUsrHis.getSpaceId().equals(space.getId())) {
                    spaceVo.setCurrentSpace(space);
                    return;
                }
            }
        }}
        Space space = spaces.get(0);
        docUsrHis.setSpaceId(space.getId());
        userDocInfoEventService.setHisId(docUsrHis, HisIdEnum.SPACE);
        spaceVo.setCurrentSpace(space);
    }

    @Override
    public Space addSpace(Space space) {
        return spaceService.insert(space);
    }

    @Override
    public SpaceEnvironmentVo getEnv(Integer spaceId) {
        SpaceEnvironmentVo result = new SpaceEnvironmentVo();

        DocUsrHis usrLastInfo = userDocInfoEventService.getUsrLastInfoBySpace(spaceId);
        List<SpaceEnvironment> envList = environmentServiceImpl.queryBySpaceId(spaceId);
        if (CollectionUtils.isEmpty(envList)) {
            SpaceEnvironment environment = new SpaceEnvironment();
            environment.setEnableFlag(1).setEnvDesc("默认创建开发环境").setEnvName("开发环境").setEnvHeader("")
                    .setEnvHost("localhost").setEnvPort(80).setServerPriority(1).setSpaceId(usrLastInfo.getSpaceId());
            environmentServiceImpl.insert(environment);
            result.setCurrent(environment).setAll(Collections.singletonList(environment));
        } else {
            result.setAll(envList);
            for (SpaceEnvironment spaceEnvironment : envList) {
                if (spaceEnvironment.getId().equals(usrLastInfo.getEnvId())) {
                    result.setCurrent(spaceEnvironment);
                    return result;
                }
            }
            result.setCurrent(envList.get(0));
        }
        return result;
    }
}
